﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Tebbee.UI.Client.AdvertiseSer;
using Tebbee.UI.Client.CategorySer;

namespace Tebbee.UI.Client.Controllers.GuestApi
{
    public class RecommendController : ApiController
    {
        private readonly AdvertiseSerClient db = new AdvertiseSerClient();
        private readonly CategorySerClient dbx = new CategorySerClient();

        public IEnumerable<Advertise_TOP_Result> GetTopView()
        {
            var tmp = db.GetAdvertiseTop("View");
            return tmp;
        }

        public IEnumerable<Advertise_TOP_Result> GetTopLike()
        {
            return db.GetAdvertiseTop("Like");
        }

        public IEnumerable<Advertise_TOP_Result> GetTopNew()
        {
            return db.GetAdvertiseTop("New");
        }

        [HttpPost]
        public HttpResponseMessage GetCategoryList(CategoryRequest request)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            int totalPage = 0; //Initial variable to get totalpage after get data from server
            IEnumerable<Category_Search_Result> list = dbx.Category_Search(out totalPage, request.SearchString, request.CurrentPage);
            //Return list point and total page to client
            return Request.CreateResponse(HttpStatusCode.OK, new { list, totalPage });
        }

        [HttpPost]
        public HttpResponseMessage GetAdvertiseByCatId(CategoryRequest request)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            int totalPage = 0; //Initial variable to get totalpage after get data from server
            IEnumerable<Advertise_GetByCatId_Result> list = db.Advertise_GetByCatId(out totalPage, request.SearchString, request.CurrentPage, request.CatId);
            //Return list point and total page to client
            return Request.CreateResponse(HttpStatusCode.OK, new { list, totalPage });
        }
    }

    public class CategoryRequest
    {
        public string SearchString { get; set; }
        public int CurrentPage { get; set; }
        public int CatId { get; set; }
    }
}
